package com.huan.mvc.ontrollor;

import com.huan.mvc.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class ParamController {
    @RequestMapping("/testServletAPI")
    //形参位置的request表示当前请求
    public String testServletAPI(HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username = " + username+"password = " + password);
        return "success";
    }

    //形参中的参数名和请求中的参数名对应即可
    //若请求参数中出现多个同名的请求参数
    //可以在控制器方法的形参位置设置字符串类型或字符串数组来接收此请求参数
    //若使用字符串类型的形参，最终结果为请求参数的每一个值之间使用逗号隔开
    //若形参中的参数名和请求中的参数名不一致则得不到对应的参数值
    @RequestMapping("/testParam")
    public String testParam(
            @RequestParam(
                    value = "user_name", //将user_name映射到username
                    required = false, //设置不是必须的，即可以不传该参数
                    defaultValue = "abc" //设置默认值
            ) String username,
            String password,
            String[] hobby,
            @RequestHeader(
                    value = "sayHaha",required = true,defaultValue = "haha"
            ) String host,
            @CookieValue("JSESSIONID") String JSESSIONID
    ){
        System.out.println("=========================>我我我");
        if(username.equals("我我我")){
            System.out.println("=========================>");
        }
        System.out.println("username="+username+
                ",password="+password+",hobby="+hobby);
        System.out.println("host = " + host);
        System.out.println("JSESSIONID = " + JSESSIONID);
        return "success";
    }

    @RequestMapping("/testBean")
    public String testBean(User user){
        System.out.println("user = " + user);
        return "success";
    }
}

